<?php
/**
 * 后台角色模型
 * AdminRole.php
 * ==============================================
 * 版权所有 2013-2014
 * ----------------------------------------------
 * 这不是一个自由软件，未经授权不许任何使用和传播。
 * ----------------------------------------------
 * 
 * @package xcs
 * @author Huangdaohai <huangdaohai.2007@163.com>
 * @version $Id$
 * @access public
 */
class AdminRole extends AdminModel
{
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return AdminRole the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return '{{admin_role}}';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('role_name, description', 'required'),
			array('status', 'numerical', 'integerOnly'=>true),
			array('role_name', 'length', 'max'=>30),
			array('action_list', 'safe'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('role_id, role_name, action_list, description, status', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'role_id' => 'Role',
			'role_name' => '角色名称',
			'action_list' => '权限列表',
			'description' => '权限描述',
			'status' => '状态',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('role_id',$this->role_id);
		$criteria->compare('role_name',$this->role_name,true);
		$criteria->compare('action_list',$this->action_list,true);
		$criteria->compare('description',$this->description,true);
		$criteria->compare('status',$this->status);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
	//将所有角色具有的权限写入缓存
	public static function set_role_auth_cache($role_id){
		//得到所有的权限缓存
		$role_auth = Yii::app()->filecache->get('role_auth_cache');
		$role_auth[$role_id]=array();
		$role_list = Yii::app()->db->createCommand('select action_list from {{admin_role}} where role_id="'.$role_id.'"')->queryRow();
		if (!empty($role_list['action_list'])){
			$sql = "select * from {{admin_menu}} where menu_id in(".$role_list['action_list'].")";
			$re = Yii::app()->db->createCommand($sql)->queryAll();
			foreach ($re as $v){
				$role_auth[$role_id][$v['menu_id']] = $v['module_id'].'|'.$v['controller_id'].'|'.$v['action_id'];
			}
		}
		//更新知道的角色权限缓存
		Yii::app()->filecache->set('role_auth_cache', $role_auth, 24*3600);
		return $role_auth[$role_id];
	}
	//将所有角色具有的权限写入缓存
	public static function get_role_auth_cache($role_id){
		$auth=Yii::app()->filecache->get('role_auth_cache');
		if(isset($auth[$role_id]) && !empty($auth)){
			$role_auth = $auth[$role_id];
		}else{
			$role_auth = AdminRole::set_role_auth_cache($role_id);
		}
		return $role_auth;
	}
}